package com.lk.api.app;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import com.lk.api.R;
import com.lk.api.entity.Users;

/**
 * Created by lk on 2014/12/25.
 */
public class SelectSqlite3 extends ListActivity {
    DBOperation dbOperation;
    ListView listView;
    int mPosition;
    Cursor cursor;
    //    数据库ID
    int _id;
    Users users;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a);
        registerForContextMenu(getListView());
        dbOperation = new DBOperation(SelectSqlite3.this);
        dbOperation.openOrCreateDatabase();

        //        显示数据添加到集合
        displayUsers();

        return_id();
        returns();




    }

   public int return_id(){
       cursor.moveToPosition(mPosition);
       //    通过_ID字段获取索引
       int mColumnIndex = cursor.getColumnIndex("_id");
       _id = cursor.getInt(mColumnIndex);
       return _id;
    }

    public Users returns(){
        cursor.moveToPosition(mPosition);
        //    通过_ID字段获取索引
        int mColumnIndex = cursor.getColumnIndex("_id");
        _id = cursor.getInt(mColumnIndex);
        return users;

    }

    //  初始化的数据 和插入方法
    private void displas() {
        Users users = new Users();
        users.setNames("SqLite3使用,请点击我不放操作");
        users.setPwd("");
        dbOperation.insertData(users);

    }

    private void displayUsers() {
        cursor = dbOperation.selectAllData();
//        初始化数据
        if (cursor.getCount() == 0) {
            displas();
        }

        // 建立连接adapter
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
                R.layout.useradapter, cursor, new String[]{"names",
                "pwd"}, new int[]{R.id.UserAdaptertextView1,
                R.id.UserAdaptertextView2});
        // 设置
        setListAdapter(cursorAdapter);

// 长按事件
        listView = getListView();
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(SelectSqlite3.this, "" + position, Toast.LENGTH_LONG).show();
                mPosition = position;
                return false;

            }
        });

    }


    //打开activity的时候 创建
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.menu_api, menu);
        Log.d("onCreateOptionsMenu", "onCreateOptionsMenu");
        return super.onCreateOptionsMenu(menu);
    }

    //打开activity的时候 调用
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        Log.d("onPrepareOptionsMenu", "onPrepareOptionsMenu");
        return super.onPrepareOptionsMenu(menu);
    }

    /*
      * 长按
      * */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        getMenuInflater().inflate(R.menu.menu_api, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    /*
    * 长按 弹出菜单 并且点击调用
    * */
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Log.d("getTitle", "" + item.getTitle());

        if (item.getTitle().equals("del")) {
            AlertDialog alertDialog = new AlertDialog.Builder(SelectSqlite3.this)
                    .setTitle("是否删除")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage("你确定删除用户嘛！")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dbOperation.deleteData(_id);
                            dialog.cancel();
                            Toast.makeText(SelectSqlite3.this, "删除成功", Toast.LENGTH_LONG).show();
                            displayUsers();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create();
            alertDialog.show();
        } else if (item.getTitle().equals("save")) {
            Intent intent = new Intent();
            intent.setClass(SelectSqlite3.this, Save.class);
            intent.putExtra("mPosition", mPosition + "");
            startActivity(intent);

        } else if (item.getTitle().equals("upd")) {
            Intent intent = new Intent();
            Bundle b=new Bundle();
            intent.setClass(SelectSqlite3.this, Upd.class);


            cursor.moveToPosition(mPosition);
            //    通过_ID字段获取索引
            int mColumnIndex = cursor.getColumnIndex("_id");
            int mNames=cursor.getColumnIndex("names");
            int pPwd = cursor.getColumnIndex("pwd");

            int upd_id = cursor.getInt(mColumnIndex);
            String names=cursor.getString(mNames);
            String pwd=cursor.getString(pPwd);
//传数组
            b.putStringArray("Upd_id", new String[]{upd_id+"", names,pwd});
            startActivity(intent);




//            Intent intent = new Intent();
//            intent.setClass(SelectSqlite3.this, Upd.class);
//            intent.putExtra("Upd_id", _id);
//            startActivity(intent);

        }


        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//        Log.d("onContextItemSelected:", "" + info.position);
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onStart() {
        Log.d("onStart", "onStart");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Log.d("onRestart", "onRestart");

        super.onRestart();
    }

    @Override
    protected void onResume() {
        Log.d("onResume", "onResume");
        displayUsers();
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d("onPause", "onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d("onStop", "onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d("onDestroy", "onDestroy");
        super.onDestroy();
    }


}
